package com.organization

import grails.test.*

class ProjectIntegrationTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testAddProject() {
		def dep = new Department(name:"BU WAC")
        assertNotNull dep.save()
        assertNotNull dep.id

		def proj1 = new Project(name:"Tektronix",startDate:new Date())
		dep.addToProjects(proj1)
		def proj2 = new Project(name:"GSM OAM",startDate:new Date())
		dep.addToProjects(proj2)
		def proj3 = new Project(name:"Synapsense",startDate:new Date())
		dep.addToProjects(proj3)
        def foundProj = Project.findByName("Tektronix")
        assertNotNull foundProj.id
		assertEquals 3,Department.get(dep.id).projects.size()
    }
	
	void testAccessAndUpdateProjects() {
		def dep = new Department(name:"BU WAC").save()
		dep.addToProjects(new Project(name:"Tektronix",startDate:new Date()))
		dep.addToProjects(new Project(name:"GSM OAM",startDate:new Date()))
		dep.addToProjects(new Project(name:"Synapsense",startDate:new Date()))
		def foundDep = Department.get(dep.id)
		def projectNames = foundDep.projects.collect{it.name}
		assertEquals(["GSM OAM","Synapsense","Tektronix"],projectNames.sort())

        def foundProj = Project.findByName("Tektronix")
        assertNotNull foundProj.id

        foundProj.name = "Tek"
        assertNotNull foundProj.save()

        projectNames = foundDep.projects.collect{it.name}
        assertEquals(["GSM OAM","Synapsense","Tek"],projectNames.sort())
    }

    void testDeleteProject(){
        def dep = new Department(name:"BU WAC").save()
        dep.addToProjects(new Project(name:"Tektronix",startDate:new Date()))
        dep.addToProjects(new Project(name:"GSM OAM",startDate:new Date()))
        dep.addToProjects(new Project(name:"Synapsense",startDate:new Date()))
        def foundProj = Project.findByName("Synapsense")
        assertNotNull foundProj.id

        foundProj.delete()
        assertFalse Project.exists(foundProj.id)

        //delete foreign key
        def foundDep = Department.get(dep.id)
        foundDep.delete()
        assertFalse Department.exists(foundDep.id)
        assertEquals 0, Project.findAll().size()
     }

    void testEvilSave() {
        def dep = new Department(name:"BU IP").save()
        def proj = new Project(department: dep, name: "T",description: "12345678901234567890123")
        assertFalse proj.validate()
        assertTrue proj.hasErrors()
        assertNull proj.save()

        def errors = proj.errors
        assertEquals "size.toosmall",errors.getFieldError("name").code
        assertEquals "T",errors.getFieldError("name").rejectedValue
        assertEquals "nullable",errors.getFieldError("startDate").code
        assertEquals null,errors.getFieldError("startDate").rejectedValue
        assertEquals "maxSize.exceeded",errors.getFieldError("description").code
        assertEquals "12345678901234567890123",errors.getFieldError("description").rejectedValue

        proj.name = "Tek"
        proj.startDate = new Date()
        proj.description = "small description"
        assertTrue(proj.validate())
        assertFalse(proj.hasErrors())
        assertNotNull proj.save()
    }
}
